package com.itheima.web.controller.system.module;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Module;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ModuleServlet",value = "/system/module")
public class ModuleServlet extends BaseServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if ("list".equals(operation)) {
            this.list(request, response);
        } else if ("toAdd".equals(operation)) {
            this.toAdd(request, response);
        } else if ("save".equals(operation)) {
            this.save(request, response);
        }else if("toEdit".equals(operation)){
            this.toEdit(request, response);
        } else if ("edit".equals(operation)) {
            this.edit(request, response);
        }else if ("delete".equals(operation)) {
            this.delete(request, response);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // ModuleService moduleService= new ModuleServiceImpl();
        Module module = BeanUtil.fillBean(request, Module.class);
        moduleService.delete(module);
        response.sendRedirect(request.getContextPath()+"/system/module?operation=list");
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Module module = BeanUtil.fillBean(request, Module.class, "yyyy-MM-dd");
        // ModuleService moduleService= new ModuleServiceImpl();
        moduleService.update(module);
        response.sendRedirect(request.getContextPath()+"/system/module?operation=list");
    }


    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        // ModuleService moduleService = new ModuleServiceImpl();
        Module module = moduleService.findById(id);
        List<Module> all = moduleService.findAll();
        request.setAttribute("moduleList",all);
        request.setAttribute("module",module);
        request.getRequestDispatcher("/WEB-INF/pages/system/module/update.jsp").forward(request, response);
    }


    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ModuleService moduleService = new ModuleServiceImpl();
        Module module = BeanUtil.fillBean(request, Module.class, "yyyy-MM-dd");
        moduleService.save(module);
        response.sendRedirect(request.getContextPath()+"/system/module?operation=list");
    }


    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Module> all = moduleService.findAll();
        request.setAttribute("moduleList",all);
        request.getRequestDispatcher("/WEB-INF/pages/system/module/add.jsp").forward(request, response);
    }


    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // ModuleService moduleService = new ModuleServiceImpl();
        int page = 1;
        int size = 100;
        if (StringUtils.isNotBlank(request.getParameter("page"))) {
            page = Integer.parseInt(request.getParameter("page"));
        }
        if (StringUtils.isNotBlank(request.getParameter("size"))) {
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = moduleService.findAll(page, size);
        request.setAttribute("page", all);
        request.getRequestDispatcher("/WEB-INF/pages/system/module/list.jsp").forward(request, response);
    }
}